cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //    default: null,      // The default value will be used only when the component attaching
        //                           to a node for the first time
        //    url: cc.Texture2D,  // optional, default is typeof default
        //    serializable: true, // optional, default is true
        //    visible: true,      // optional, default is true
        //    displayName: 'Foo', // optional
        //    readonly: false,    // optional, default is false
        // },
        // ...
        _isCapturing: false,
        _gameresult: cc.Node
    },

    // use this for initialization
    onLoad: function () {
    },

    // called every frame, uncomment this function to activate update callback
    // update: function (dt) {

    // },

    init: function () {
        this.ANDROID_API = "com/lpyx/game/WXAPI";
        this.IOS_API = "AppController";
    },

    login: function () {
        if (cc.sys.os == cc.sys.OS_ANDROID) {
            jsb.reflection.callStaticMethod(this.ANDROID_API, "Login", "()V");
        }
        else if (cc.sys.os == cc.sys.OS_IOS) {
            jsb.reflection.callStaticMethod(this.IOS_API, "login");
        }
        else {
            console.log("platform:" + cc.sys.os + " dosn't implement share.");
        }
    },

    share: function (title, desc) {
        if (cc.sys.os == cc.sys.OS_ANDROID) {
            jsb.reflection.callStaticMethod(this.ANDROID_API, "Share", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V", cc.vv.SI.appweb, title, desc);
        }
        else if (cc.sys.os == cc.sys.OS_IOS) {
            jsb.reflection.callStaticMethod(this.IOS_API, "share:shareTitle:shareDesc:", cc.vv.SI.appweb, title, desc);
        }
        else {
            console.log("platform:" + cc.sys.os + " dosn't implement share.");
        }
    },

    shareline: function (title, desc) {
        if (cc.sys.os == cc.sys.OS_ANDROID) {
            jsb.reflection.callStaticMethod(this.ANDROID_API, "ShareWxTimeline", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V", cc.vv.SI.appweb, title, desc);
        }
        else if (cc.sys.os == cc.sys.OS_IOS) {
            jsb.reflection.callStaticMethod(this.IOS_API, "shareWxTimeline:shareTitle:shareDesc:", cc.vv.SI.appweb, title, desc);
        }
        else {
            console.log("platform:" + cc.sys.os + " dosn't implement share.");
        }
    },
    shopping: function (title, desc) {
        if (cc.sys.os == cc.sys.OS_ANDROID) {
            jsb.reflection.callStaticMethod(this.ANDROID_API, "ShareWxTimeline", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V", cc.vv.SI.appweb, title, desc);
        }
        else if (cc.sys.os == cc.sys.OS_IOS) {
            jsb.reflection.callStaticMethod(this.IOS_API, "shareWxTimeline:shareTitle:shareDesc:", cc.vv.SI.appweb, title, desc);
        }
        else {
            console.log("platform:" + cc.sys.os + " dosn't implement share.");
        }
    },
    shareResult: function () {
        // if (this._isCapturing) {
        //     return;
        // }
        this._isCapturing = true;
        var size = cc.view.getVisibleSize();
        var renderTexture = cc.RenderTexture.create(size.width, size.height, cc.Texture2D.PIXEL_FORMAT_RGBA8888, gl.DEPTH24_STENCIL8_OES);
        //实际截屏的代码
        renderTexture.begin();
        cc.director.getRunningScene().visit();
        renderTexture.end();
        var fullPath = jsb.fileUtils.getWritablePath() + "share.jpg";
        jsb.fileUtils.isFileExist(fullPath) && jsb.fileUtils.removeFile(fullPath);
        renderTexture.saveToFile("share.jpg", cc.ImageFormat.JPG, true, function () {
            console.log("capture screen successfully!filePath:" + fullPath);
            if (callback)
                callback(fullPath)
        });
        var self = this;
        var tryTimes = 0;
        var fn = function () {
            if (jsb.fileUtils.isFileExist(fullPath)) {
                var height = 100;
                var scale = height / size.height;
                var width = Math.floor(size.width * scale);

                if (cc.sys.os == cc.sys.OS_ANDROID) {
                    jsb.reflection.callStaticMethod(self.ANDROID_API, "ShareIMG", "(Ljava/lang/String;)V", fullPath);
                }
                else if (cc.sys.os == cc.sys.OS_IOS) {
                    jsb.reflection.callStaticMethod(self.IOS_API, "shareIMG:width:height:", fullPath, width, height);
                }
                else {
                    console.log("platform:" + cc.sys.os + " dosn't implement share.");
                }
                self._isCapturing = false;
            }
            else {
                tryTimes++;
                if (tryTimes > 10) {
                    console.log("time out...");
                    return;
                }
                setTimeout(fn, 500);
            }
        }
        setTimeout(fn, 500);
    },

    onLoginResp: function (code) {
        var fn = function (ret) {
            if (ret.errcode == 0) {
                cc.sys.localStorage.setItem("wx_account", ret.account);
                cc.sys.localStorage.setItem("wx_sign", ret.sign);
            }
            cc.vv.userMgr.onAuth(ret);
        }
        cc.vv.http.sendRequest("/wechat_auth", { code: code, os: cc.sys.os }, fn);
    },
});
